#import "SEGUploadmultipartfileManager.h"

#if __has_include(<Commom/SEGModel.h>)
#import <Commom/SEGModel.h>
#else
#import "SEGModel.h"
#endif

static id _uploadMultipartFileManager;
static dispatch_once_t onceToken;

@interface SEGUploadmultipartfileManager()
@property (nonatomic, copy) NSString *urlString;
@end

@implementation SEGUploadmultipartfileManager
#pragma mark - SINGLE----BEGIN----
+(instancetype)sharedManager{
    dispatch_once(&onceToken, ^{
        _uploadMultipartFileManager = [[self alloc] init];
    });
    return _uploadMultipartFileManager;
}

+(void)destroyResource{
    onceToken = 0;
    _uploadMultipartFileManager = nil;
}

#pragma mark - SINGLE----END----

#pragma mark - HTTP----BEGIN----
-(void)requestDatasWithDetailData:(NSArray<SEGUploadFileModel*>*)detailData progress:(void(^)(double progressValue))progress completed:(void(^)(BOOL isSuccessed,id result,NSString* codeType,NSString* errorMessage))completed{
    
    [SEGHTTPBusinessManager seg_uploadDatasWithURLString:self.urlString parameters:nil detailSources:detailData progress:progress completed:^(BOOL isSuccessed, id result, NSString *codeType, NSError *error) {
        if (!isSuccessed || result == nil) {
            completed(NO,nil,codeType,error.localizedDescription);
            return;
        }
        
        if (![result isKindOfClass:[NSDictionary class]]) {
            completed(NO,nil,codeType,error.localizedDescription);
            return;
        }
        
        self.uploadMultipartFile = [SEGUploadmultipartfileModel yy_modelWithDictionary:result];
        completed(YES,result,codeType,error.localizedDescription);
        return;
    }];
}

-(void)requestVoicesWithData:(NSArray<NSData*>*)data progress:(void(^)(double progressValue))progress completed:(void(^)(BOOL isSuccessed,id result,NSString* codeType,NSString* errorMessage))completed{
    [SEGHTTPBusinessManager seg_uploadVoicesWithURLString:self.urlString parameters:nil sources:data progress:progress completed:^(BOOL isSuccessed, id result, NSString *codeType, NSError *error) {
        if (!isSuccessed || result == nil) {
            completed(NO,nil,codeType,error.localizedDescription);
            return;
        }
        
        if (![result isKindOfClass:[NSDictionary class]]) {
            completed(NO,nil,codeType,error.localizedDescription);
            return;
        }
        
        self.uploadMultipartFile = [SEGUploadmultipartfileModel yy_modelWithDictionary:result];
        completed(YES,result,codeType,error.localizedDescription);
        return;
    }];
}

-(void)requestVoiceWithData:(NSData*)data progress:(void(^)(double progressValue))progress completed:(void(^)(BOOL isSuccessed,id result,NSString* codeType,NSString* errorMessage))completed{
    
    [SEGHTTPBusinessManager seg_uploadVoiceWithURLString:self.urlString parameters:nil source:data progress:progress completed:^(BOOL isSuccessed, id result, NSString *codeType, NSError *error) {
        if (!isSuccessed || result == nil) {
            completed(NO,nil,codeType,error.localizedDescription);
            return;
        }
        
        if (![result isKindOfClass:[NSDictionary class]]) {
            completed(NO,nil,codeType,error.localizedDescription);
            return;
        }
        
        self.uploadMultipartFile = [SEGUploadmultipartfileModel yy_modelWithDictionary:result];
        completed(YES,result,codeType,error.localizedDescription);
        return;
    }];
}

-(void)requestVoiceWithData:(NSData*)data fileName:(NSString*)fileName progress:(void(^)(double progressValue))progress completed:(void(^)(BOOL isSuccessed,id result,NSString* codeType,NSString* errorMessage))completed{
    
    [SEGHTTPBusinessManager seg_uploadVoiceWithURLString:self.urlString parameters:nil source:data fileName:fileName progress:progress completed:^(BOOL isSuccessed, id result, NSString *codeType, NSError *error) {
        if (!isSuccessed || result == nil) {
            completed(NO,nil,codeType,error.localizedDescription);
            return;
        }
        
        if (![result isKindOfClass:[NSDictionary class]]) {
            completed(NO,nil,codeType,error.localizedDescription);
            return;
        }
        
        self.uploadMultipartFile = [SEGUploadmultipartfileModel yy_modelWithDictionary:result];
        completed(YES,result,codeType,error.localizedDescription);
        return;
    }];
}

#pragma mark - HTTP----END----

#pragma mark - GETTER----BEGIN----
-(SEGUploadmultipartfileModel *)uploadMultipartFile{
    if (!_uploadMultipartFile) {
        _uploadMultipartFile = [[SEGUploadmultipartfileModel alloc]init];
    }
    return _uploadMultipartFile;
}

-(NSString *)urlString{
    if (!_urlString) {
        _urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/uhomecp-app/common/uploadMultipartFile.json",nil];
    }
    return _urlString;
}
#pragma mark - GETTER----END----
@end
